package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.modular.api.mapper.WXUserMapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.entity.SysUserRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wenchuan on 2017/6/16.
 */
@Service
public class WXUserService {

//    @Autowired
//    private Gwaa_tDao gwaa_tDao;
//
//    @Autowired
//    private Sys_userDao sys_userDao;
//
//    @Autowired
//    private Sys_user_roleDao sys_user_roleDao;

    @Autowired
    WXUserMapper wxUserMapper;


    /**
     * 根据用户状态获取用户列表信息
     *
     * @param params
     * @return
     */
    public List<Dto> getUserList(Page<Object>page,Dto params) {
        return wxUserMapper.getUserPage(page,params);
    }

    /**
     * 获取用户信息
     *
     * @param params
     * @return
     */
    public GwaaT getUserData(Dto params) {
        GwaaT gwaaT = CommonUtils.toJavaClass(params, GwaaT.class);

        return gwaaT.selectOne(new QueryWrapper<GwaaT>().allEq(params));
    }

//    /**
//     * 获取权限用户列表
//     *
//     * @param params
//     * @return
//     */
//    public List<Dto> getSysUserList(Dto params) {
//        return sqlDao.list("Home.selectModulesOfUser", params);
//    }



    /**
     * 根据客户编号获取区域，根据区域获取对应的维修部门
     *
     * @param params
     * @return
     */
    public List<Dto> getWxbByUserCodeList(Dto params) {
        List<Dto> list = wxUserMapper.getWxbByUserCodeList(params);
        return list;
    }

    /**
     * 根据客户编号获取 客户资料表gwak_t_gwakl_t
     *
     * @return
     */
    public Dto getGwakByKey(String gwakent, String gwak001) {

        Dto dto = new Dto();
        dto.put("GWAKENT", gwakent);
        dto.put("GWAK001", gwak001);

        return wxUserMapper.getGwakByKey(dto);
    }
    /**
     * 根据有效码更新用户数据
     *
     * @return
     */
    @Transactional
    public void updateByKeyActi(Dto param) {
        //WHERE GWAAENT = #{dto.GWAAENT} AND GWAA001 = #{dto.} AND GWAAACTI=#{dto.GWAAACTI}
        UpdateWrapper<GwaaT> wrapper = new UpdateWrapper<>();
        wrapper.eq("GWAAENT",param.getString("GWAAENT"));
        wrapper.eq("GWAA001",param.getString("GWAA001"));
        wrapper.eq("GWAAACTI",param.getString("GWAAACTI_Old"));

        GwaaT gwaaT = CommonUtils.toJavaClass(param, GwaaT.class);
        gwaaT.update(wrapper);
    }


    public List<Dto> getGwaaByZcsh(Dto dto) {
        return wxUserMapper.getGwaaByZcsh(dto);
    }

    /**
     * 用户审核
     *
     * @return
     */
    @Transactional
    public void checkUserSave(Dto gwaaDto, SysUser sysUser, SysUserRole sys_user_rolePO) {

        updateByKeyActi(gwaaDto);
        sysUser.update(new UpdateWrapper<SysUser>().eq("ID",sysUser.getID()));
        sys_user_rolePO.insert();
    }

    /**
     * 用户登陆
     *
     * @return
     */
    @Transactional
    public Dto login(Dto gwaaDto) {
        Dto user = wxUserMapper.checkUser(gwaaDto);
        return user;
    }
}
